package io.renren.modules.sp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.sp.dao.*;
import io.renren.modules.sp.dto.SpTeacherCerImport;
import io.renren.modules.sp.entity.SpBaseMatchItemEntity;
import io.renren.modules.sp.entity.SpCerTeacherImportEntity;
import io.renren.modules.sp.entity.SpMatchEntity;
import io.renren.modules.sp.entity.SpUserEntity;
import io.renren.modules.sp.service.SpTeacherCerImportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 选手荣誉证书导入
 */
@Service
public class SpTeacherCerImportServiceImpl implements SpTeacherCerImportService {

    @Autowired
    private SpUserDao spUserDao;
    @Autowired
    private SpMatchDao spMatchDao;
    @Autowired
    private SpBaseMatchItemDao spBaseMatchItemDao;
    @Autowired
    private SpCerTeacherImportDao spCerTeacherImportDao;


    /**
     * 导入获奖证书校验
     * @param
     */
    @Override
    public SpTeacherCerImport checkCertificate(SpTeacherCerImport spCerTeacherImport){
        // 校验赛事名称是否存在
        if(StringUtils.isBlank(spCerTeacherImport.getMatchName())){
            spCerTeacherImport.setCheckMsg("赛事不存在");
            return spCerTeacherImport;
        }
        List<SpMatchEntity> matchList = spMatchDao.selectList(new QueryWrapper<SpMatchEntity>().eq("match_name",spCerTeacherImport.getMatchName()));
        if(matchList == null || matchList.size()==0) {
            spCerTeacherImport.setCheckMsg("赛事不存在");
            return spCerTeacherImport;
        }

        // 校验手机号和身份证不能同时为空
        if(StringUtils.isBlank(spCerTeacherImport.getTeacher()) && StringUtils.isBlank(spCerTeacherImport.getIdCard())){
            spCerTeacherImport.setCheckMsg("手机号和身份证不能同时为空");
            return spCerTeacherImport;
        }
        // 如果手机号不为空根据手机号校验是否存在当前用户
        if(StringUtils.isNotBlank(spCerTeacherImport.getTeacher())){
            SpUserEntity user = spUserDao.selectOne(new QueryWrapper<SpUserEntity>().eq("phone",spCerTeacherImport.getTeacher()));
            if(user == null){
                //手机号不存在，再校验身份证
                SpUserEntity userIdCard = spUserDao.selectOne(new QueryWrapper<SpUserEntity>().eq("id_card",spCerTeacherImport.getIdCard()));
                if(userIdCard == null) {
                    spCerTeacherImport.setCheckMsg("手机号和身份证不存在");
                    return spCerTeacherImport;
                }else{
                    //匹配身份证号与姓名是否一致
                    if(!userIdCard.getRealName().equals(spCerTeacherImport.getUserName())){
                        spCerTeacherImport.setCheckMsg("身份证和姓名不匹配");
                        return spCerTeacherImport;
                    }
                }
            }else{
                if(!user.getRealName().equals(spCerTeacherImport.getUserName())){
                    spCerTeacherImport.setCheckMsg("手机号和姓名不匹配");
                    return spCerTeacherImport;
                }else{
                    spCerTeacherImport.setUserId(user.getId());
                }
                //手机号不匹配，再校验身份证
                SpUserEntity userIdCard = spUserDao.selectOne(new QueryWrapper<SpUserEntity>().eq("id_card",spCerTeacherImport.getIdCard()));
                if(userIdCard == null) {
                    spCerTeacherImport.setCheckMsg("身份证不存在");
                    return spCerTeacherImport;
                }else{
                    //匹配身份证号与姓名是否一致
                    if(!userIdCard.getRealName().equals(spCerTeacherImport.getUserName())){
                        spCerTeacherImport.setCheckMsg("身份证和姓名不匹配");
                        return spCerTeacherImport;
                    }else {
                        spCerTeacherImport.setUserId(userIdCard.getId());
                    }
                }
            }
        }else{
            // 如果手机号为空根据身份证校验是否存在当前用户
            List<SpUserEntity> userList = spUserDao.selectList(new QueryWrapper<SpUserEntity>().eq("id_card",spCerTeacherImport.getIdCard()));
            if(userList == null || userList.size()==0){
                spCerTeacherImport.setCheckMsg("身份证不存在");
                return spCerTeacherImport;
            }else if(userList.size()>1){
                spCerTeacherImport.setCheckMsg(spCerTeacherImport.getIdCard()+"身份证重复");
                return spCerTeacherImport;
            } else {
                SpUserEntity user = userList.get(0);
                if(!user.getRealName().equals(spCerTeacherImport.getUserName())){
                    spCerTeacherImport.setCheckMsg("身份证和姓名不匹配");
                    return spCerTeacherImport;
                }
                spCerTeacherImport.setUserId(user.getId());
            }
        }
        // 校验赛项名称是否存在
        SpBaseMatchItemEntity spBaseMatchItemEntity = spBaseMatchItemDao.selectOne(
                new QueryWrapper<SpBaseMatchItemEntity>()
                        .eq("item_name",spCerTeacherImport.getBaseMatchItemName())
        );
        if(spBaseMatchItemEntity == null){
            spCerTeacherImport.setCheckMsg("基础赛项名称不存在");
            return spCerTeacherImport;
        }
        // 校验证书编号是否和库内重复
        List<SpCerTeacherImportEntity> certificateList = this.spCerTeacherImportDao.selectList(
                new QueryWrapper<SpCerTeacherImportEntity>()
                        .eq("certificate",spCerTeacherImport.getCertificate())
        );
        if(certificateList!=null && certificateList.size()>0){
            spCerTeacherImport.setCheckMsg(spCerTeacherImport.getCertificate() + "证书编号重复");
            return spCerTeacherImport;
        }

        return spCerTeacherImport;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCertificateImport(List<SpTeacherCerImport> spCerTeacherImport){
        SpCerTeacherImportEntity spCerTeacherImportEntity = null;
        for (SpTeacherCerImport importDto : spCerTeacherImport) {
            spCerTeacherImportEntity = new SpCerTeacherImportEntity();
            BeanUtils.copyProperties(importDto, spCerTeacherImportEntity);
            spCerTeacherImportEntity.setPhone(importDto.getTeacher());
            spCerTeacherImportDao.insert(spCerTeacherImportEntity);
        }
    }


}
